/*
 * HideTableColumnListener.java
 *
 * Created on May 11, 2007, 10:51 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package com.bff.slimserver.musicjukebox.ui.components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;


/**
 *
 * @author bill
 */
public class HideTableColumnListener extends MouseAdapter
        implements ActionListener {
    private JTable table = null;
    private JPopupMenu hideColumnPopup = null;
    private HashMap menuItemColumnMap = null;
    private HashMap columnWidthMap = null;
    
    //private int[] hideColumns = {};
    /*
    CallListTableModel.TEAM_CODE,
    CallListTableModel.APPLICATION_CATEGORY,
    CallListTableModel.CALL_PTS,
    CallListTableModel.ESCALATED_TO_EMPNO};
     */
    
    /**
     * Default Class Constructor.
     * @param table the JTable to which the listener is been added.
     */
    public HideTableColumnListener(JTable table) {
        this.table = table;
        columnWidthMap = new HashMap();
        initHiddenColumns();
        table.getTableHeader().addMouseListener( this );
    }
    
    public void initHiddenColumns() {
        TableColumnModel model = table.getColumnModel();
        /*
        for (int j = 0; j < hideColumns.length; j++) {
            columnWidthMap.put( model.getColumn(hideColumns[j]), new Integer(model.getColumn(hideColumns[j]).getPreferredWidth()));
            model.getColumn(hideColumns[j]).setMinWidth( 0 );
            model.getColumn(hideColumns[j]).setPreferredWidth(0);
        }
         */
    }
    
    
    /**
     * Method used to dispose of system resources allocated by the class. It
     * removes all Listeners from the JTableHeader and the JCheckBoxMenuItems.
     * <br><br> <b><i>THIS METHOD MUST BE CALLED WHEN THE ASSOCIATED JTABLE
     * OBJECT IS NO LONGER IN USE (table is been disposed).</i></b>
     */
    public void dispose() {
        if( null != table ) {
            table.getTableHeader().removeMouseListener( this );
            table = null;
        }
        
        if( null != menuItemColumnMap ) {
            Iterator list = menuItemColumnMap.keySet().iterator();
            
            while( list.hasNext() ) {
                ((JCheckBoxMenuItem)list.next()).removeActionListener(this);
            }
            
            menuItemColumnMap.clear();
            columnWidthMap.clear();
        }
    }
    
    
    
    /**
     * Method called by the garbage collector when it determines that there are
     * no more references to the object. It calls the dispose() method to
     * dispose of system resources.
     * @exception Throwable - the Exception raised by this method
     */
    protected void finalize() throws Throwable {
        dispose();
    }
    
    /**
     * Implementation of ActionListener interface used by the menu items to
     * hide columns.
     * @param evt an ActionEvent
     */
    public void actionPerformed(ActionEvent evt) {
        Object source = evt.getSource();
        TableColumn column = null;
        
        if( null != (column = (TableColumn)menuItemColumnMap.get(source)) ) {
            JCheckBoxMenuItem item = (JCheckBoxMenuItem)source;
            
            if( item.isSelected() ) {
                Integer i = null;
                
                if( null != (i = (Integer)columnWidthMap.get(column)) ) {
                    column.setPreferredWidth(i.intValue());
                }
            } else {
                column.setPreferredWidth(0);
            }
        }
    }
    
    
    /**
     * Implementation of MouseListener interface used to show the popup menu.
     * @param e a MouseEvent
     */
    public void mouseClicked(MouseEvent e) {
        if(((e.getModifiers() & e.BUTTON3_MASK) != 0) || e.isPopupTrigger()) {
            if( (null == hideColumnPopup) ) {
                initPopupMenu();
            }
            
            hideColumnPopup.show( e.getComponent(), e.getX(), e.getY() );
        }
    }
    
    /**
     * Initialize the JPopupMenu.
     */
    private void initPopupMenu() {
        hideColumnPopup = new JPopupMenu("Show Columns");
        
        TableColumnModel model = table.getColumnModel();
        TableColumn column = null;
        JCheckBoxMenuItem item = null;
        
        menuItemColumnMap = new HashMap();
        
        
        int len = table.getColumnCount();
        for(int i=0; i<len; i++) {
            column = model.getColumn( i );
            column.setMinWidth( 0 );
            
            String str = column.getHeaderValue().toString();
            
            item = new JCheckBoxMenuItem(str);
            item.setSelected( true );
            item.addActionListener(this);
            hideColumnPopup.add( item );
            /*
            for (int j = 0; j < hideColumns.length; j++) {
                if(i == hideColumns[j]) {
                    item.setSelected(false);
                    column.setPreferredWidth( 0 );
                    break;
                }
            }
             */
            
            if(!columnWidthMap.containsKey(column)) {
                columnWidthMap.put( column, new Integer(column.getPreferredWidth()) );
            }
            
            menuItemColumnMap.put( item, column );
        }
    }
    
} // End class HideTableColumnListener
